using Spine.Unity;
using System;
using System.Collections;
using TMPro;
using UnityEngine;

public class Transporter : MonoBehaviour
{
	private Animator animator;

	private Elevator elevator;

	private Warehouse warehouse;

	private Vector3 gatheringPoint;

	private Vector3 exploitedPoint;

	private Vector3 restingPosition;

	private Transform myselfTransform;

	private Transform animatorTransform;

	private int skinID;

	private float loadingTime;

	private float walkingSpeed;

	private float skinEffective;

	private bool isIdle = true;

	private double totalCashTransfer;

	private double cashAfterTransfer;

	private double cashBeforeTransfer;

	[SerializeField]
	private GameObject mineral;

	[SerializeField]
	private GameObject cashIcon;

	[SerializeField]
	private TextMeshPro cashLabel;

	[SerializeField]
	private MineralSprite[] mineralSprite;

	public bool IsIdle => isIdle;

	private void Awake()
	{
		myselfTransform = base.transform;
		restingPosition = myselfTransform.position;
		animator = GetComponentInChildren<Animator>();
		animatorTransform = animator.transform.parent;
	}

	public void Initialize(Elevator elevator, Warehouse warehouse)
	{
		this.elevator = elevator;
		this.warehouse = warehouse;
		gatheringPoint = warehouse.GatheringPoint;
		exploitedPoint = warehouse.ExploitedPoint;
		SpriteRenderer component = mineral.GetComponent<SpriteRenderer>();
		MineralSprite mineralSprite = Array.Find(this.mineralSprite, (MineralSprite target) => target.mineral == Controller.instance.GetMineral);
		component.sprite = mineralSprite.sprite;
		Collectible instance = Collectible.instance;
		instance.changeTransporterSkin = (Action<int>)Delegate.Combine(instance.changeTransporterSkin, new Action<int>(ChangeSkin));
		Collectible.Properties property = Collectible.instance.Property;
		ChangeSkin(property.transporterSkin);
	}

	private void ChangeSkin(int skinID)
	{
		this.skinID = skinID;
		switch (skinID)
		{
		case 0:
			GetComponentInChildren<SkeletonAnimator>().skeleton.SetSkin("miner3");
			break;
		case 1:
			GetComponentInChildren<SkeletonAnimator>().skeleton.SetSkin("miner3Epic");
			break;
		case 2:
			GetComponentInChildren<SkeletonAnimator>().skeleton.SetSkin("miner3Legend");
			break;
		}
		skinEffective = Collectible.instance.GetSkinEffective(Collectible.SkinTYPE.Transporter);
	}

	public void StartTransport()
	{
		isIdle = false;
		StartCoroutine(Transport());
	}

	private void Animate(string clip, float speed)
	{
		if (clip != null && clip == "Walk")
		{
			Warehouse.Properties property = warehouse.Property;
			walkingSpeed = property.walkingSpeed * speed;
		}
		animator.speed = speed;
		animator.Play(clip);
	}

	private IEnumerator Transport()
	{
		Warehouse.WarehouseBoost boosts = warehouse.Boosts;
		Animate("Walk", boosts.walkingSpeed);
		totalCashTransfer = 0.0;
		while (myselfTransform.position != exploitedPoint)
		{
			myselfTransform.position = Vector3.MoveTowards(myselfTransform.position, exploitedPoint, Time.deltaTime * walkingSpeed);
			yield return null;
		}
		Elevator.Properties property = elevator.Property;
		cashBeforeTransfer = property.cash;
		if (cashBeforeTransfer > 0.0)
		{
			elevator.Collect(value: true);
			Animate("Idle", 1f);
			double num = cashBeforeTransfer;
			Warehouse.Properties property2 = warehouse.Property;
			double loadPerTransporter = property2.loadPerTransporter;
			Warehouse.WarehouseBoost boosts2 = warehouse.Boosts;
			float num3;
			if (num <= loadPerTransporter * (double)boosts2.loadExpansion * (double)skinEffective)
			{
				double num2 = cashBeforeTransfer;
				Warehouse.Properties property3 = warehouse.Property;
				double loadingSpeed = property3.loadingSpeed;
				Warehouse.WarehouseBoost boosts3 = warehouse.Boosts;
				num3 = (float)(num2 / (loadingSpeed * (double)boosts3.loadingSpeed * (double)skinEffective));
			}
			else
			{
				Warehouse.Properties property4 = warehouse.Property;
				double loadPerTransporter2 = property4.loadPerTransporter;
				Warehouse.WarehouseBoost boosts4 = warehouse.Boosts;
				double num4 = loadPerTransporter2 * (double)boosts4.loadExpansion * (double)skinEffective;
				Warehouse.Properties property5 = warehouse.Property;
				double loadingSpeed2 = property5.loadingSpeed;
				Warehouse.WarehouseBoost boosts5 = warehouse.Boosts;
				num3 = (float)(num4 / (loadingSpeed2 * (double)boosts5.loadingSpeed * (double)skinEffective));
			}
			loadingTime = num3;
			yield return new WaitForSeconds(loadingTime);
			Elevator.Properties property6 = elevator.Property;
			cashAfterTransfer = property6.cash;
			if (cashAfterTransfer > 0.0)
			{
				double num5 = cashAfterTransfer;
				Warehouse.Properties property7 = warehouse.Property;
				double loadPerTransporter3 = property7.loadPerTransporter;
				Warehouse.WarehouseBoost boosts6 = warehouse.Boosts;
				double num6;
				if (num5 > loadPerTransporter3 * (double)boosts6.loadExpansion * (double)skinEffective)
				{
					Warehouse.Properties property8 = warehouse.Property;
					double loadPerTransporter4 = property8.loadPerTransporter;
					Warehouse.WarehouseBoost boosts7 = warehouse.Boosts;
					num6 = loadPerTransporter4 * (double)boosts7.loadExpansion * (double)skinEffective;
				}
				else
				{
					num6 = cashAfterTransfer;
				}
				totalCashTransfer = num6;
				elevator.SetCash(0.0 - totalCashTransfer);
				cashIcon.SetActive(value: true);
				cashLabel.text = Utilities.ConvertCash(totalCashTransfer);
				mineral.SetActive(value: true);
			}
			elevator.Collect(value: false);
		}
		animatorTransform.eulerAngles += Vector3.up * 180f;
		Warehouse.WarehouseBoost boosts8 = warehouse.Boosts;
		Animate("Walk", boosts8.walkingSpeed);
		while (myselfTransform.position != gatheringPoint)
		{
			myselfTransform.position = Vector3.MoveTowards(myselfTransform.position, gatheringPoint, Time.deltaTime * walkingSpeed);
			yield return null;
		}
		Animate("Idle", 1f);
		double num7 = totalCashTransfer;
		Warehouse.Properties property9 = warehouse.Property;
		double loadingSpeed3 = property9.loadingSpeed;
		Warehouse.WarehouseBoost boosts9 = warehouse.Boosts;
		yield return new WaitForSeconds((float)(num7 / (loadingSpeed3 * (double)boosts9.loadingSpeed * (double)skinEffective)));
		warehouse.Cash(totalCashTransfer);
		cashLabel.text = string.Empty;
		cashIcon.SetActive(value: false);
		mineral.SetActive(value: false);
		animatorTransform.eulerAngles += Vector3.up * 180f;
		if (warehouse.Profiles == null)
		{
			Warehouse.WarehouseBoost boosts10 = warehouse.Boosts;
			Animate("Walk", boosts10.walkingSpeed);
			while (myselfTransform.position != restingPosition)
			{
				myselfTransform.position = Vector3.MoveTowards(myselfTransform.position, restingPosition, Time.deltaTime * walkingSpeed);
				yield return null;
			}
			if (warehouse.Profiles == null)
			{
				isIdle = true;
				warehouse.IdleTransporter();
				animator.Play("Idle", 0, 0f);
			}
			else
			{
				StartCoroutine(Transport());
			}
		}
		else
		{
			StartCoroutine(Transport());
		}
	}
}
